package cn.itcast.controlobj;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private TextView textView;
    private Button btn_one, btn_two, btn_three;
    private int textsize = 1;
    private int[] textSizeArr = {10,20,25,30,40};
    private CharSequence[] items = new CharSequence[]{"旅游","美食","看电影","运动"};
    private boolean[] checkedItems = new boolean[]{false, true, false, false};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_one = (Button) findViewById(R.id.btn_1);
        btn_two = (Button) findViewById(R.id.btn_2);
        btn_three = (Button) findViewById(R.id.btn_3);
        textView = (TextView) findViewById(R.id.tv_info);
        btn_two.setOnClickListener(this);
        btn_three.setOnClickListener(this);
        /*btn_two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn_two.setText("按钮2被点击");
            }
        });*/
    }

    public void click(View view){
        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle("设置字体大小")
                .setIcon(R.mipmap.ic_launcher)
                .setSingleChoiceItems(new String[]{"小号", "默认", "中号", "大号", "特大"}, textsize,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                textsize = which;
                            }
                        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        textView.setTextSize(textSizeArr[textsize]);
                        dialog.dismiss();
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        dialog = builder.create();
        dialog.show();
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.btn_2:
                popupDialog();
                break;
            case R.id.btn_3:
                popupMultiDialog();
                Toast.makeText(MainActivity.this,"WIFI已断开",Toast.LENGTH_SHORT).show();
                break;
        }
    }

    private void popupDialog(){
        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle("普通对话框")
                .setIcon(R.mipmap.ic_launcher).setMessage("是否退出应用：")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        MainActivity.this.finish();
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        dialog = builder.create();
        dialog.show();
    }

    private void popupMultiDialog(){
        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle("设置字体大小")
                .setIcon(R.mipmap.ic_launcher)
                .setMultiChoiceItems(items, checkedItems,
                        new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                checkedItems[which] = isChecked;
                            }
                        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        StringBuffer stringBuffer = new StringBuffer();
                        for(int i=0; i<= checkedItems.length-1; i++){
                            if(checkedItems[i])
                                stringBuffer.append(items[i]).append(" ");
                    }
                        if(stringBuffer != null){
                            Toast.makeText(MainActivity.this, stringBuffer, Toast.LENGTH_SHORT).show();
                        }
                }
    }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        dialog = builder.create();
        dialog.show();
    }
}